﻿using System;
using System.ComponentModel;
using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace BCS.Utils
{
    [DefaultProperty("SMTPServer")]
    public class Correo: IDisposable
    {
        #region Variables Privadas
        private int _Port;
        private string _SmtpServer;
        private bool _EnableSSL;
        private int _TimeOut;
        private SmtpDeliveryMethod _DeliveryMethod;
        private bool _UseDefaultCredentials;
        private NetworkCredential _Credentials;
        #endregion

        #region Propiedades Publicas
        public int Port {
            get { return _Port; }
            set { _Port = value; }
        }
        
        public string SMTPServer
        {
            get { return _SmtpServer; }
            set { _SmtpServer = value; }
        }

        public bool EnableSSL
        {
            get { return _EnableSSL; }
            set { _EnableSSL = value; }
        }

        public int TimeOut
        {
            get { return _TimeOut; }
            set { _TimeOut = value; }
        }

        public SmtpDeliveryMethod DeliveryMethod
        {
            get { return _DeliveryMethod; }
            set { _DeliveryMethod = value; }
        }

        public bool UseDefaultCredentials
        {
            get { return _UseDefaultCredentials; }
            set { _UseDefaultCredentials = value; }
        }

        public NetworkCredential Credentials
        {
            get { return _Credentials; }
            set { _Credentials = value; }
        }
        #endregion

        #region Constructores
        public Correo()
        {
            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["Mail.SMTPServer"]))
            {
                var tmp = ConfigurationManager.AppSettings["Mail.SMTPServer"].Split(Convert.ToChar(":"));

                if (tmp.Length >= 2)
                {
                        _SmtpServer = tmp[0];
                        _Port = Convert.ToInt32(tmp[1]);
                }
            }

            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["Mail.UseDefaultCredentials"]))
                _UseDefaultCredentials = Convert.ToBoolean(ConfigurationManager.AppSettings["Mail.UseDefaultCredentials"]);

            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["Mail.EnableSSL"]))
                _EnableSSL = Convert.ToBoolean(ConfigurationManager.AppSettings["Mail.EnableSSL"]);

            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["Mail.DeliveryMethod"]))
            {
                if (ConfigurationManager.AppSettings["Mail.DeliveryMethod"].Equals("Network"))
                    _DeliveryMethod = SmtpDeliveryMethod.Network;
                if (ConfigurationManager.AppSettings["Mail.DeliveryMethod"].Equals("PickupDirectoryFromIis"))
                    _DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
                if (ConfigurationManager.AppSettings["Mail.DeliveryMethod"].Equals("SpecifiedPickupDirectory"))
                    _DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
            }
                
            _TimeOut = !string.IsNullOrEmpty(ConfigurationManager.AppSettings["Mail.TimeOut"]) ? Convert.ToInt32(ConfigurationManager.AppSettings["Mail.TimeOut"]) : 10000;

            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["Mail.Credentials"]))
            {
                var tmp = ConfigurationManager.AppSettings["Mail.Credentials"].Split(Convert.ToChar("|"));

                if (tmp.Length >= 2)
                    _Credentials = new NetworkCredential(tmp[0], MD5.DecryptString(tmp[1], ConfigurationManager.AppSettings["Sys.LlavePublica"]));
            }
            else
                _Credentials = null;
        }

        public Correo (string smtpServer, int port)
        {
            _Port = port;
            _SmtpServer = smtpServer;
        }
        
        public Correo (string smtpServer, int port, bool enableSSL)
        {
            _Port = port;
            _SmtpServer = smtpServer;
            _EnableSSL = enableSSL;
        }
 
        public Correo (string smtpServer, int port, bool enableSSL, NetworkCredential credential)
        {
            _Port = port;
            _SmtpServer = smtpServer;
            _EnableSSL = enableSSL;
            _Credentials = credential;
        }
        #endregion

        #region Metodos Publicos
        public bool Enviar(string emailFrom, string emailTO, string subject, string body)
        {
            var ss = new SmtpClient(_SmtpServer, _Port)
                         {
                             EnableSsl = _EnableSSL,
                             Timeout = _TimeOut,
                             DeliveryMethod = _DeliveryMethod,
                             UseDefaultCredentials = _UseDefaultCredentials,
                             Credentials = _Credentials
                         };

            MailMessage mm = new MailMessage(emailFrom, emailTO, subject, body) {BodyEncoding = Encoding.UTF8};
            ss.Send(mm);

            return true;
        }

        public bool Enviar(MailMessage mensaje)
        {
            var ss = new SmtpClient(_SmtpServer, Convert.ToInt32(_Port))
            {
                EnableSsl = _EnableSSL,
                Timeout = _TimeOut,
                DeliveryMethod = _DeliveryMethod,
                UseDefaultCredentials = _UseDefaultCredentials,
                Credentials = _Credentials
            };

            ss.Send(mensaje);

            return true;
        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
        #endregion
    }
}